package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        Integer userId = user.getId();
        if(userId==0){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
//        if (apBehaviorEntry == null) {
//            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
//        }
        if (dto.getType() == 0) {
            saveUnLikeArticle(apBehaviorEntry, dto);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else if (dto.getType() == 1) {
            deleteUnLikeArticle(apBehaviorEntry, dto);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    private void deleteUnLikeArticle(ApBehaviorEntry apBehaviorEntry, UnLikesBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
        ApUnlikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (apLikesBehavior != null) {
            mongoTemplate.remove(query,ApUnlikesBehavior.class);
        }else{
            CustException.cust(AppHttpCodeEnum.NEED_UnLike);
        }
    }

    /**
     * 不喜欢操作
     * @param apBehaviorEntry
     * @param dto
     */
    private void saveUnLikeArticle(ApBehaviorEntry apBehaviorEntry, UnLikesBehaviorDTO dto) {
        Query query = Query.query(Criteria.where("entryId")
                .is(apBehaviorEntry.getId())
                .and("articleId")
                .is(dto.getArticleId()));
        ApUnlikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
//        System.err.println("apLikesBehavior = " + apLikesBehavior);
        if (apLikesBehavior == null) {
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            Long articleId = dto.getArticleId();
//            System.err.println("articleId = " + articleId);
            apUnlikesBehavior.setArticleId(articleId);
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
            apUnlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apUnlikesBehavior);
        }else{
            CustException.cust(AppHttpCodeEnum.NEED_UnLike);
        }
    }

}
